package com.kafka.service;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import net.sf.json.JSONObject;

import com.kafka.common.Constant;
import com.kafka.core.BaseProducer;
import com.kafka.enumtype.MsgType;
import com.kafka.producer.AccountProducer;
import com.kafka.producer.PayProducer;
import com.kafka.producer.UserProducer;
import com.kafka.util.BaseUtil;

/**
 * 跨中心操作接口
 * 
 * @author 雷洪岸
 * 
 */
@Path("dc")
public class ApiResource extends BaseProducer{
	private static PayProducer pp = new PayProducer();
	private static UserProducer up = new UserProducer();
	private static AccountProducer ap = new AccountProducer();
	
	
	@Path("/user/doc/update")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateUser(@Valid String datas){
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		JSONObject message = createDCMessage(JSONObject.fromObject(datas),MsgType.UDATE);
		JSONObject response = new JSONObject();
		try {
			JSONObject result = JSONObject.fromObject(up.produce(message));
			if(result.getBoolean("success")==true){
				response.put("status", Constant.REQUEST_SUCCESS);
				response.put("message", "ok");
				return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
			}else{
				response.put("status", Constant.REQUEST_FAIL);
				response.put("message", result.getString("msg"));
				return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
			}
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
	
	@Path("/user/account/unbind")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response unbindAccount(@Valid String datas) {
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		JSONObject message = createDCMessage(JSONObject.fromObject(datas),MsgType.UNBINDACCOUNT);
		JSONObject response = new JSONObject();
		try {
			ap.dcUnbindIccard(message);
			response.put("status", Constant.REQUEST_SUCCESS);
			response.put("message", "ok");
			return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
	
	@Path("/pay/pay")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public String pay(@Valid String datas) {
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		String send = null;
		JSONObject message = rebuildJSON(JSONObject.fromObject(datas));
		if(!Constant.GOODSTYPE.equals(message.getString("goodstype"))){
			send = pp.dcPay(message).toString();
			log.info("###ApiResource send:" + send + ",time:" + BaseUtil.getDate());
			return send;
		} else{
			message = createDCMessage(message,MsgType.RECHARGE);
			send = pp.dcRecharge(message).toString();
			log.info("###ApiResource send:" + send + ",time:" + BaseUtil.getDate());
			return send;
		}
	}
	
	@Path("/user/account/xpasswd")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updatePayPasswd(@Valid String datas){
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		JSONObject message = createDCMessage(JSONObject.fromObject(datas),MsgType.UPDATEPAYPASSWD);
		JSONObject response = new JSONObject();
		try {
			JSONObject result = JSONObject.fromObject(ap.updatePayPasswd(message));
			if(result.getBoolean("success")==true){
				response.put("status", Constant.REQUEST_SUCCESS);
				response.put("message", "ok");
				return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
			}else{
				response.put("status", Constant.REQUEST_FAIL);
				response.put("message", result.getString("msg"));
				return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
			}
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
	
	@Path("/user/doc/sets")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response docSets(@Valid String datas){
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		// 先获取数据集中的status判断请求类型
		JSONObject tmp = rebuildJSON(JSONObject.fromObject(datas));
		JSONObject message = new JSONObject();
		JSONObject response = new JSONObject();
		if(Constant.USERACTIVATE == tmp.getInt("status")){
			message = createDCMessage(tmp,MsgType.ACTIVATEUSER);
		}
		if(Constant.USERBLACKLIST == tmp.getInt("status")){
			message = createDCMessage(tmp,MsgType.USERBLACKLIST);
		}
		if(Constant.USERCLOSE == tmp.getInt("status")){
			message = createDCMessage(tmp,MsgType.CLOSEUSER);
		}
		try {
			JSONObject result = JSONObject.fromObject(up.produce(message));
			if(result.getBoolean("success")==true){
				response.put("status", Constant.REQUEST_SUCCESS);
				response.put("message", "ok");
				return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
			}else{
				response.put("status", Constant.REQUEST_FAIL);
				response.put("message", result.getString("msg"));
				return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
			}
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
	
	@Path("/user/account/sets")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response accountSets(@Valid String datas){
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		// 先获取数据集中的status判断请求类型
		JSONObject tmp = rebuildJSON(JSONObject.fromObject(datas));
		JSONObject message = new JSONObject();
		JSONObject response = new JSONObject();
		if(Constant.ACCOUNTBLACKLIST == tmp.getInt("status")){
			message = createDCMessage(tmp,MsgType.CARDBLACKLIST);
		}
		if(Constant.ACCOUNTACTIVATE == tmp.getInt("status")){
			message = createDCMessage(tmp,MsgType.ACTIVATECARD);
		}
		try {
			JSONObject result = JSONObject.fromObject(ap.produce(message));
			if(result.getBoolean("success")==true){
				response.put("status", Constant.REQUEST_SUCCESS);
				response.put("message", "ok");
				return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
			}else{
				response.put("status", Constant.REQUEST_FAIL);
				response.put("message", result.getString("msg"));
				return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
			}
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
	
	@Path("/user/account/lost")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response cardLost(@Valid String datas){
		log.info("###ApiResource:" + datas + ",time:" + BaseUtil.getDate());
		// 先获取数据集中的status判断请求类型
		JSONObject tmp = rebuildJSON(JSONObject.fromObject(datas));
		JSONObject message = new JSONObject();
		JSONObject response = new JSONObject();
		message = createDCMessage(tmp,MsgType.LOSTCARD);
		try {
			JSONObject result = JSONObject.fromObject(ap.produce(message));
			if(result.getBoolean("success")==true){
				response.put("status", Constant.REQUEST_SUCCESS);
				response.put("message", "ok");
				return Response.status(Constant.REQUEST_SUCCESS).entity(response.toString()).build(); 
			}else{
				response.put("status", Constant.REQUEST_FAIL);
				response.put("message", result.getString("msg"));
				return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
			}
		} catch (Exception e) {
			response.put("status", Constant.REQUEST_FAIL);
			response.put("message", e.toString());
			return Response.status(Constant.REQUEST_FAIL).entity(response.toString()).build(); 
		}
	}
}
